/*******************************************************************************
  * Copyright(c) 2023 Levetop Semiconductor Co.,Led. All rights reserved.   
  * @file     tftlcd.h
  * @author   UartTFT Application Team
  * @version  V0.0.1
  * @date     2023-01-09
  * @brief    This file contains the interface initialization and basic
  *           control functions of the TFT.
********************************************************************************/ 

#ifndef _TFTLCD_H
#define _TFTLCD_H

#include "module_select.h"
#include "eport_drv.h"
#include "spi_drv.h"
#include "pwm_drv.h"

//----------------------LCD_SPI_Port------------------------------
#if (LT_TFT_PORT==0) 

	#define		LCD_RST_RESET			PWM->PWM_PCR_PDR &=~(1<<PWM_PORT1)  //EPORT1->EPDR &=~(1<<5);
	#define 	LCD_RST_SET				PWM->PWM_PCR_PDR |= (1<<PWM_PORT1)  //EPORT1->EPDR |= (1<<5);
		
	#define 	LCD_CS_RESET        SPI2->SPIPORT &=~(1<<SPI_SS)
	#define 	LCD_CS_SET          SPI2->SPIPORT |= (1<<SPI_SS)

#define  LCD_RS_CMD	  SPI2->SPIPORT &=~(1<<SPI_MISO)
#define  LCD_RS_DATA  SPI2->SPIPORT |= (1<<SPI_MISO)

	#define 	LCD_CLK_RESET		I2C->PDR &= ~(1<<I2C_SCL)      //not used
	#define 	LCD_CLK_SET			I2C->PDR |=  (1<<I2C_SCL)      //not used
	
	#define 	LCD_SDA_RESET		I2C->PDR &= ~(1<<I2C_SDA)      //not used
	#define 	LCD_SDA_SET			I2C->PDR |=  (1<<I2C_SDA)      //not used
#define  LCD_SDA_READ   (I2C->PDR>>=1)
	
#endif

//----------------------LCD_PPI------------------------------            
#if (LT_TFT_PORT==1||LT_TFT_PORT==2) 

	#define		LCD_RST_RESET			PWM->PWM_PCR_PDR &=~(1<<PWM_PORT1)  //EPORT1->EPDR &=~(1<<5) 
	#define 	LCD_RST_SET				PWM->PWM_PCR_PDR |= (1<<PWM_PORT1)  //EPORT1->EPDR |= (1<<5) 
	
	#define 	LCD_CS_RESET			SPI2->SPIPORT &=~(1<<SPI_MISO)
	#define 	LCD_CS_SET				SPI2->SPIPORT |= (1<<SPI_MISO)

#define  LCD_RS_CMD   SPI2->SPIPORT &=~(1<<SPI_SS)
#define  LCD_RS_DATA  SPI2->SPIPORT |= (1<<SPI_SS)

	#define 	LCD_RD_RESET			SPI2->SPIPORT &=~(1<<SPI_SCK)
	#define 	LCD_RD_SET				SPI2->SPIPORT |= (1<<SPI_SCK)
	
	#define 	LCD_WR_RESET			SPI2->SPIPORT &=~(1<<SPI_MOSI)
	#define 	LCD_WR_SET				SPI2->SPIPORT |= (1<<SPI_MOSI)

#define  LCD_WR_READ_L  SPI2->SPIPORT&0xFD
#define  LCD_WR_READ_H  SPI2->SPIPORT|0x02
#define  LCD_WR_WRITE   SPI2->SPIPORT
	
#if (LT_TFT_PORT==1) 

#define  LCD_DATA_WRITE_H  EPORT->EPDR           //Write
#define  LCD_DATA_WRITE_L  EPORT->EPDR           //Write

#define  LCD_DATA_READ_H  EPORT->EPPDR          //Read 
#define  LCD_DATA_READ_L  EPORT->EPPDR          //Read 

#define  LCD_PIN_OUTPUT  EPORT->EPDDR  = 0xff;
#define  LCD_PIN_INPUT   EPORT->EPDDR  = 0x00;
#endif
	
#if (LT_TFT_PORT==2) 

#define  LCD_DATA_WRITE_H  EPORT3->EPDR           //Write
#define  LCD_DATA_WRITE_L  EPORT->EPDR            //Write

#define  LCD_DATA_READ_L  EPORT->EPPDR           //Read 

#define  LCD_PIN_OUTPUT  { EPORT3->EPDDR = 0xff; EPORT->EPDDR  = 0xff; }
#define  LCD_PIN_INPUT   { EPORT3->EPDDR = 0x00; EPORT->EPDDR  = 0x00; }
	
#endif

#endif



void LCD_RS(uint8_t bit);
void LCD_CS(uint8_t bit);

//LCD_SPI
void LCD_SPI_Port_Init(void);
uint8_t LCD_SPI_ReadWriteByte(uint8_t Send);
void LCD_SPI_WriteData_DMA(uint8_t *Tx_Addr,uint32_t length);

//LCD_PPI
void LCD_PPI_Port_Init(void);
void TFT_CommandWrite(uint8_t cmd);
void TFT_DataWrite(uint8_t data);
void TFT_DataRead(uint16_t* buf,uint32_t numpixel) ;
uint32_t TFT_DataReadReg(uint8_t reg,uint8_t NumtoRead);

//LCD_Init
void LCD_TFT_Init(void);
void LT268_TFT_SetWindows(uint16_t XStar,uint16_t yStar,uint16_t Width,uint16_t High);

void ST7789VPanelSleepInMode(void);
void ST7789VPanelSleepOutMode(void);
void LT268_Display_ON(void);
void LT268_Display_OFF(void);

//LCD_pin_test
void LCD_PIN_TEST(void);


#endif	/* _TFTLCD_H */
